package org.example.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.com.Bean.Category;
import org.example.com.Bean.User;
import org.example.com.exception.userRemoveException;
import org.example.com.exception.userSaveException;
import org.example.com.mapper.CategoryMapper;
import org.example.com.service.CategoryService;
import org.example.com.util.JsonResult;
import org.example.com.util.RecultTool;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Override
    public JsonResult findCategory(){
        return RecultTool.success(list());
    }
    @Override
    public JsonResult findCategory(int page){
        Page<Category> p= Page.of(page, 10);
        List<Category> list=list(p);
        p.setRecords(list);
        return RecultTool.success(p);
    }

    @Override
    public JsonResult saveCategory(Category category) {
        boolean save = save(category);
        if (!save) {
            throw new userSaveException("添加用户失败");
        }
        return RecultTool.success("success");
    }
    @Override
    public JsonResult updateCategory(Category category) {
        boolean b = updateById(category);
        if (!b) {
            throw new userSaveException("修改用户失败");

        }
        return RecultTool.success("success");
    }

    @Override
    public JsonResult deleteCategory(String id) {
        boolean b = removeById(id);
        if (!b) {
            throw new userSaveException("修改用户失败");

        }
        return RecultTool.success("success");
    }
    @Override
    public JsonResult updateCategoryStatus(String id, int status) {
        UpdateWrapper<Category> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("status", status);
        boolean b = update(wrapper);
        if (!b) {
            throw new userRemoveException("修改状态失败");
        }
        return RecultTool.success("success");
    }

}